/*
 * Copyright 2006-2022 ahtelit.com, All Rights Reserved.
 */

package com.telit.firecommand.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.telit.firecommand.dao.CommFireAlarmDao;
import com.telit.firecommand.dao.CommMsgCenterLogDao;
import com.telit.firecommand.dao.SysUserDAO;
import com.telit.firecommand.dto.CommMsgCenterLog;
import com.telit.firecommand.entity.CommFireAlarm;
import com.telit.firecommand.entity.SysUser;
import com.telit.firecommand.service.CommMsgCenterLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * CommMsgCenterLogServiceImpl
 *
 * @author yuezhuang
 * @since 2023-02-15
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class CommMsgCenterLogServiceImpl implements CommMsgCenterLogService {

    private final SysUserDAO sysUserDAO;

    private final CommMsgCenterLogDao commMsgCenterLogDao;

    private final CommFireAlarmDao commFireAlarmDao;

    /**
     * 生成火情消息记录
     *
     * @param mobileList    手机号列表
     * @param commFireAlarm 火情信息
     */
    @Override
    public void saveFireMsg(List<String> mobileList, CommFireAlarm commFireAlarm) {
        //查询用户id
        List<SysUser> sysUsers = sysUserDAO.selectByMobile(mobileList);
        List<Long> idList = sysUsers.stream().map(SysUser::getId).collect(Collectors.toList());

        List<CommMsgCenterLog> commMsgCenterLogs = new ArrayList<>();
        String content = commFireAlarm.getDistrictName() + "发现了真实火情,请及时组织扑火工作";
        String title = "火情报警";
        idList.forEach(e -> commMsgCenterLogs.add(
                new CommMsgCenterLog(content, "4", new Date(), "1", commFireAlarm.getFinderName(),
                        title, e)));
        if (CollectionUtil.isNotEmpty(commMsgCenterLogs)) {
            commMsgCenterLogDao.addBatch(commMsgCenterLogs);
        }
    }

    @Override
    public void saveFireMsg(List<String> mobileList, Long fireId) {
        saveFireMsg(mobileList, commFireAlarmDao.selectByPrimaryKey(fireId));
    }

}
